package ugc

import (
	"encoding/json"
	"sensitives/service/dic"
	"strconv"

	"github.com/gin-gonic/gin"
)

type live struct {
	common
}

// Live 直播
var Live live

func (filter *live) Comment(cx *gin.Context) {
	siteid, _ := strconv.Atoi(cx.Param("siteid"))
	content := cx.PostForm("content")
	type response struct {
		Code bool        `json:"code"`
		Data []sensitive `json:"data"`
	}
	filter.SensWords = make([]sensitive, 0)
	output := response{Code: false, Data: filter.SensWords}
	if content == "" {
		sensUser, _ := json.Marshal(output)
		cx.Writer.Write([]byte(sensUser))
	}
	userDic := dic.CloudMap[siteid].User
	filter.filter(content, "content", &userDic)
	output = response{Code: true, Data: filter.SensWords}
	sensUser, _ := json.Marshal(output)
	cx.Writer.Write(sensUser)
}

func (filter *live) Post(cx *gin.Context) {
	siteid, _ := strconv.Atoi(cx.Param("siteid"))
	text := cx.PostForm("text")
	type response struct {
		Code bool        `json:"code"`
		Data []sensitive `json:"data"`
	}
	filter.SensWords = make([]sensitive, 0)
	output := response{Code: false, Data: filter.SensWords}
	if text == "" {
		sensUser, _ := json.Marshal(output)
		cx.Writer.Write([]byte(sensUser))
	}
	userDic := dic.CloudMap[siteid].User
	filter.filter(text, "text", &userDic)
	output = response{Code: true, Data: filter.SensWords}
	sensUser, _ := json.Marshal(output)
	cx.Writer.Write(sensUser)
}
